package com.ruoyi.project.per.app.type.controller;

import com.ruoyi.common.constant.TypeConstants;
import com.ruoyi.common.exception.cms.CMSTypeNotDeleteException;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.per.app.type.domain.AppType;
import com.ruoyi.project.per.app.type.service.IAppTypeService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.service.IPostService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 小新
 * @date 2020-09-19 11:30
 * @desc
 */
@Controller
@RequestMapping("/per/app/type")
public class AppTypeController extends BaseController {

    private String prefix = "per/app/type";

    @Autowired
    private IAppTypeService appTypeService;

    @RequiresPermissions("app:type:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/type";
    }

    @RequiresPermissions("app:type:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppType appType)
    {
        startPage();
        List<AppType> list = appTypeService.selectAppTypeList(appType);
        return getDataTable(list);

    }
    /**
     * 新增申请类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 保存新增内容
     * @param appType
     * @return
     */
    @RequiresPermissions("app:type:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(AppType appType)
    {
        if (TypeConstants.TYPE_NAME_NOT_UNIQUE.equals(appTypeService.checkTypeNameUnique(appType)))
        {
            return error("新增公告类型'" + appType.getTypeName() + "'失败，公告类型名称已存在");
        }
        return toAjax(appTypeService.insertAppType(appType));
    }

    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkTypeNameUnique(AppType appType){
        return appTypeService.checkTypeNameUnique(appType);
    }

    /**
     * 修改数据的回显
     * @param id
     * @param modelMap
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable long id, ModelMap modelMap){

        modelMap.addAttribute("AppType",appTypeService.selectAppTypeById(id));
        return prefix + "/edit";
    }
    @RequiresPermissions("app:type:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppType appType){
        if (TypeConstants.TYPE_NAME_NOT_UNIQUE.equals(appTypeService.checkTypeNameUnique(appType)))
        {
            return error("申请公告类型'" + appType.getTypeName() + "'失败，申请类型名称已存在");
        }
        return toAjax(appTypeService.editAppType(appType));
    }

    @RequiresPermissions("app:type:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Integer[] ids){
        try{
            int result = appTypeService.deleteAppTypeByIds(ids);
            return toAjax(result);
        }catch (CMSTypeNotDeleteException e){
            return AjaxResult.error(e.getMessage());
        }
    }

}
